<script setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { Plus } from '@element-plus/icons-vue'
import * as admin from '@/api/admin.js'
import { useTokenStore } from '@/stores/token.js';
const tokenStore = useTokenStore();

// 模拟数据
const posts = ref([]);
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);
const addModal = ref(false);
const updateModal = ref(false);
const search_title = ref('');
const search_content = ref('');

const updateData = ref({
  id: '',
  userId: "",
  title: "",
  content: "",
  img: "",
})

const addData = ref({
  title: "",
  content: "",
  img: "",
})

const showDialog = (row) => {
  console.log(row.title);
  updateData.value.id = row.id;
  updateData.value.title = row.title;
  updateData.value.content = row.content;
  updateData.value.img = row.img;
}

const fetchPosts = async() => {
  const param = {
      currentPage: currentPage.value,
      title: search_title.value,
      content: search_content.value,
  }
  const res = await admin.findCommunity(param)
  posts.value = res.data;
  total.value = res.total;
};

const searchPosts = () => {
  fetchPosts();
};

const deletePost = async(id) => {
  const param = {
    id: id
  }
  ElMessageBox.confirm('确认删除该社区内容？', '提示', { type: 'warning' })
    .then(async () => {
      const res = await admin.delCommunity(param)
      if (res.code == 0) {
        fetchPosts();
      }
      ElMessage.success('删除成功');
    })
    .catch(() => ElMessage.info('取消删除'));
};

const addMethod = async() => {
  const res = await admin.addCommunity(addData.value)
  if (res.code == 0) {
    fetchPosts();
  }
  ElMessage.success('增加成功');
  addModal.value = false;
};

const editMethod = async() => {
  const res = await admin.updateCommunity(updateData.value)
  if (res.code == 0) {
    fetchPosts();
  }
  ElMessage.success('修改成功');
  updateModal.value = false;
};

const handlePageChange = (page) => {
  currentPage.value = page;
  fetchPosts();
};

onMounted(() => {
  fetchPosts();
});
</script>

<template>
  <div class="page">
    <el-card class="search-box">
      <el-form :inline="true" class="search-form">
        <el-form-item label="标题">
          <el-input v-model="search_title" placeholder="请输入标题" clearable />
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="search_content" placeholder="请输入内容" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchPosts" icon="el-icon-search">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-button type="success" class="add-button" @click="addModal = true">➕ 添加社区</el-button>

    <el-table :data="posts" border>
      <el-table-column prop="title" label="标题" width="180" />
      <el-table-column prop="content" label="内容" />
      <el-table-column prop="createdAt" label="发布时间" width="150" />
      <el-table-column label="操作" width="180">
        <template #default="{ row }">
          <el-button type="primary" size="small" @click="showDialog(row),updateModal=true">修改</el-button>
          <el-button type="danger" size="small" @click="deletePost(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :total="total"
      :page-size="pageSize"
      @current-change="handlePageChange"
    />
  </div>

  <el-drawer
  v-model="updateModal"
  title="修改公告信息"
  direction="rtl"
  class="demo-drawer"
  >
  <div class="demo-drawer__content">
    <el-form :model="updateData">
        <el-form-item label="公告标题" prop="title">
          <el-input v-model="updateData.title" placeholder="请输入公告标题" />
        </el-form-item>
        <el-form-item label="公告" prop="phone">
          <el-input
            v-model="updateData.content"
            :rows="2"
            type="textarea"
            placeholder="公告内容"
          />
        </el-form-item>
        <el-form-item label="公告图片" prop="img">
          <el-upload
            class="avatar-uploader"
            action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
            :show-file-list="false"
            :on-success="uploadSuccess"
          >
            <img v-if="updateData.img" :src="updateData.img" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item>
    </el-form>
    <div class="demo-drawer__footer">
      <el-button @click="cancelForm">取消</el-button>
      <el-button type="primary" :loading="loading" @click="editMethod">提交</el-button>
    </div>
  </div>
  </el-drawer>

  <el-dialog 
  v-model="addModal" 
  title="添加公告" 
  width="30%" 
  center
  >
  <el-form 
    label-width="80px" 
    :model="addData" 
    style="padding: 10px 20px"  >
    <el-form-item label="公告标题">
      <el-input v-model="addData.title" placeholder="请输入公告标题" />
    </el-form-item>
    <el-form-item label="公告内容">
      <el-input
        v-model="addData.content"
        :rows="2"
        type="textarea"
        placeholder="公告内容"
      />
    </el-form-item>
    <el-form-item label="公告图片" prop="img">
      <el-upload
        class="avatar-uploader"
        action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
        :show-file-list="false"
        :on-success="uploadSuccess"
      >
        <img v-if="addData.img" :src="addData.img" class="avatar" />
        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
      </el-upload>
    </el-form-item>
  </el-form>

  <template #footer>
    <span class="dialog-footer">
      <el-button @click="addModal = false">取消</el-button>
      <el-button 
        type="primary" 
        @click="addMethod"
      >
        提交
      </el-button>
    </span>
  </template>
  </el-dialog>

</template>

<style scoped>
.page {
  padding: 30px;
  background: #f7f7f7;
}

.search-box {
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 20px;
  margin-bottom: 20px;
}

.add-button {
  background-color: #67c23a;
  color: white;
  border-radius: 8px;
  font-size: 14px;
}

.el-button {
  margin: 0 5px;
}

.el-table {
  margin-top: 20px;
}

.el-pagination {
  margin-top: 20px;
  text-align: center;
}
</style>
